package cst.a521wh.zyfypt_521wh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cst.a521wh.zyfypt_521wh.Model.ResModel;
import cst.a521wh.zyfypt_521wh.R;
import cst.a521wh.zyfypt_521wh.bean.LoginBean;
import cst.a521wh.zyfypt_521wh.iface.AmicoolListener;

public class LoginActivity extends AppCompatActivity  {
    private EditText et_userName, et_password;
    private Button btn_login, btn_register;
    private CheckBox checkBox_remember;
    private ResModel model;
    private AmicoolListener<LoginBean> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_password=(EditText)findViewById(R.id.et_password);
        et_userName=(EditText)findViewById(R.id.et_username);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_register=(Button)findViewById(R.id.btn_register);

        listener=new AmicoolListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if(l.getUsername()!=null){
                    String sessionid =l.getSessionid();
                    //把sessionid存到一个地方
                    Intent intent=new Intent(
                            LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();

            }
        };
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model=new ResModel("gson");
                model.login(et_userName.getText().toString(),et_password.getText().toString(),listener);

            }
        });


    }

}
